/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.MyAttachDTO;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Dang Thi Phuong Thao
 */
public class MyAttachDAO  extends AbDAO{
     private ArrayList<MyAttachDTO> capNhatTuResultSet(ResultSet rs) throws SQLException{
        ArrayList<MyAttachDTO> Kq = new ArrayList<MyAttachDTO>();
        /*String Chuoi = "";
        try {
            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                Chuoi += rs.getMetaData().getColumnLabel(i) + ", ";
            }
            JOptionPane.showMessageDialog(null, Chuoi);
        } catch (SQLException ex) {
            Logger.getLogger(MyMailDAO.class.getName()).log(Level.SEVERE, null, ex);
        }*/
        
            //index = rsmd.getColumnCount();
            int index = 0;
            while (rs.next()) {
                index = 1;
                //String temp = rs.getString(index++);
                int Id = rs.getInt(index++);
                int IdMail = rs.getInt(index++);
                String DuongDan = rs.getString(index++);
                String TenFile = rs.getString(index++);
                MyAttachDTO myContactDTO = new MyAttachDTO(Id, IdMail,DuongDan,TenFile);
                Kq.add(myContactDTO);
            }

        return Kq;
     }

     public ArrayList<MyAttachDTO> layTatCaAttach () throws SQLException, IOException{
            KetNoi();
            ResultSet rs = thucThiTraVeResultSet("select * from attach");
            ArrayList<MyAttachDTO> Kq = capNhatTuResultSet(rs);
            DongKetNoi();
            return Kq;
         }

     public ArrayList<MyAttachDTO> layTatCaAttachCuaMailID (int MailID) throws SQLException, IOException{
            KetNoi();
            ResultSet rs = thucThiTraVeResultSet("select * from attach where IDMail = " + MailID);
            ArrayList<MyAttachDTO> Kq = capNhatTuResultSet(rs);
            DongKetNoi();
            return Kq;
         }
          public int themAttach (MyAttachDTO myAttachDTO) throws IOException{
            KetNoi();
            //String date = mailDTO.getM_Ngay().toString().substring(0, "dd-MM-yyyy hh:mm:ss".length());
            String sql = "insert into attach(IDMail,DuongDan,TenFile)values(" + myAttachDTO.getM_IdMail()+ ", " + "'" + myAttachDTO.getM_DuongDan()+
                    "', " + "'" + myAttachDTO.getM_TenFile()+"'" + ")";
            int Kq = thucThiUpdate(sql);

            DongKetNoi();

            return Kq;
          }
           public int xoaAttach(MyAttachDTO attachDTO) throws IOException{
            KetNoi();
            String sql = "Delete from attach Where ID ="+ attachDTO.getM_Id();
            int kq = thucThiUpdate(sql);
            DongKetNoi();
            return kq;
    }
}
